package com.fenshi.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.android.volley.VolleyError;
import com.fenshi.bean.CarBean;
import com.fenshi.interactor.ParkInteractor;
import com.fenshi.interactor.imp.ParkInteractorImp;
import com.fenshi.manager.activity.order.BuildOrderActivity;
import com.fenshi.presenter.ParkPresenter;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.CookicUntil;
import com.fenshi.until.SystemUtils;
import com.fenshi.view.ParkView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/16.
 */
public class ParkPresenterImp implements ParkPresenter, RequestListener<List<CarBean>> {

    final int UnWork = 0;
    final int Work = 1;
    final int Rest = 2;

    ParkInteractor interactor;
    ParkView view;
    Context c;
    List<CarBean> result;

    int currentPage = 1;
    String cardId;

    public ParkPresenterImp(Context c) {
        this(c, (ParkView) c);
    }

    public ParkPresenterImp(Context c, ParkView view) {
        this.c = c;
        this.view = view;
        interactor = new ParkInteractorImp(c, this);
    }

    @Override
    public void getRestCar(String type) {
        String parkId =CookicUntil.getUser().getParkId();
        if (parkId == null) return;
        String userId = CookicUntil.getUser().getUserId();
        if (userId == null) {
            view.showError("用户未登录");
            return;
        }
        view.showLoading();
        interactor.getRestCar(userId, parkId, type, currentPage);
    }


    @Override
    public void onItemClick(int potions) {
        CarBean carBean = result.get(potions);
        if (((Activity) c).getIntent().getExtras() !=null){
            cardId = ((Activity) c).getIntent().getExtras().getString(CookicUntil.CODE);
        }
        Intent intent = new Intent(c, BuildOrderActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("car", carBean);
        bundle.putString("cardId", cardId);
        intent.putExtras(bundle);
        c.startActivity(intent);
//        ((Activity) c).finish();
    }

    @Override
    public void Success(String tag, List<CarBean> result) {
        List<Map<String, Object>> bean = new ArrayList<>();
        for (CarBean carBean : result) {
            Map<String, Object> obj = SystemUtils.beanToMap(carBean);
            bean.add(obj);
        }
        view.dismissLoad();
        view.carList(bean, currentPage == 1);
        view.notifyDateChange();
        currentPage++;
        if (this.result == null)
            this.result = result;
        else
            this.result.addAll(result);
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError("服务器异常，请稍后重试");
    }
}
